package com.bjpowernode.service.impl;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.manager.RedisManager;
import com.bjpowernode.mapper.TDicValueMapper;
import com.bjpowernode.model.TActivity;
import com.bjpowernode.model.TDicValue;
import com.bjpowernode.service.DicService;
import com.bjpowernode.util.CacheUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class DicServiceImpl implements DicService {

    @Resource
    private TDicValueMapper tDicValueMapper;

    @Resource
    private RedisManager redisManager;

    @Override
    public List<TDicValue> getDicValueByTypeCode(String typeCode) {
        List<TDicValue> tDicValueList = CacheUtils.getCacheData(
                () -> (List<TDicValue>)redisManager.getValue(Constants.REDIS_DIC_VALUE_KEY + typeCode),
                () -> tDicValueMapper.selectByTypeCode(typeCode),
                (t) -> redisManager.setValue(Constants.REDIS_DIC_VALUE_KEY + typeCode, t, Constants.OPTIONS_EXPIRE_TIME, TimeUnit.MINUTES)
        );
        return tDicValueList;
    }
}
